package com.appheader.rongmaphone.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

import com.appheader.rongmaphone.R;

public class MusicService extends Service {
    MediaPlayer player;
    //    private MySeekReceiver receiver;
    private int currentPosition;//播放位置

//    private final IBinder binder = new AudioBinder();

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //我们从raw文件夹中获取一个应用自带的mp3文件
        player = MediaPlayer.create(this, R.raw.ray_of_light);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (player.isPlaying()) {
            player.pause();
            currentPosition = player.getCurrentPosition();
        } else {
            player.seekTo(currentPosition);
            player.start();
        }
//        play(0);

//        new MyThread().start();//启动异步任务
        return super.onStartCommand(intent, flags, startId);
    }

//    private void play(int position){
////        player = new MediaPlayer();
//
//        player.reset();;
////        player.prepare();
//        player.setOnPreparedListener(new PreparedListener(position));
//
//    }

    @Override
    public void onDestroy() {
//        super.onDestroy();
        if (player.isPlaying()) {
            player.stop();
        }
        player.release();
        Log.e("MusicService", "onDestroy: -------------------" );
//        unregisterReceiver(receiver);
    }

    //为了和Activity交互，我们需要定义一个Binder对象
//   public class AudioBinder extends Binder {
//        public void pause(){
//            if (player!=null&&player.isPlaying()){
//                currentPosition = player.getCurrentPosition();
//                player.pause();
//            }else {
//                player.start();
//                player.seekTo(currentPosition);
//            }
//        }
//        public void stop(){
//            if(player!=null){
//                player.stop();
//            }
//        }
//        public MusicService getService() {
//            return MusicService.this;
//        }
//    }

    @Override
    public boolean onUnbind(Intent intent) {

        player.release();
        return super.onUnbind(intent);
    }

//    private final class PreparedListener implements MediaPlayer.OnPreparedListener {
//        private int position;
//        public PreparedListener(int position){
//            this.position=position;
//        }
//        @Override
//        public void onPrepared(MediaPlayer arg0) {
//            player.start();//开始播放；
//            if(position>0)player.seekTo(position);
//        }
//    }
    //    class MySeekReceiver extends BroadcastReceiver {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            int seekProgress = intent.getIntExtra(GlobalVars.EXTRA_CURRENT, 0);
//            if (player != null) {
//                Log.e("tag", "onReceive:---------" + seekProgress);
//                player.seekTo(seekProgress);//指定播放的位置
//            }
//        }
//    }

//    class MyThread extends Thread {
//        @Override
//        public void run() {
//            super.run();
//            int maxLen = player.getDuration();//播放总长度
//            while (player != null && player.isPlaying()) {
//                int progress = player.getCurrentPosition();//得到当前播放的进度
//                Intent intent = new Intent(GlobalVars.ACTION_PLAY_PRG);
//                intent.putExtra(GlobalVars.EXTRA_MAX, maxLen);
//                intent.putExtra(GlobalVars.EXTRA_CURRENT, progress);
//                //发送广播
//                sendBroadcast(intent);
//            }
//        }
//    }
}
